<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="false"
      label-width="68px"
    >
      <el-form-item label="查询语句ID" prop="sqlId">
        <el-input
          v-model="queryParams.sqlId"
          placeholder="请输入查询语句ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="条件ids" prop="conditionIds">
        <el-input
          v-model="queryParams.conditionIds"
          placeholder="请输入条件ids"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子条件ids" prop="childIds">
        <el-input
          v-model="queryParams.childIds"
          placeholder="请输入子条件ids"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关系" prop="relationship">
        <el-input
          v-model="queryParams.relationship"
          placeholder="请输入关系"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['conf:BiSqlWhereRelationship:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['conf:BiSqlWhereRelationship:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['conf:BiSqlWhereRelationship:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['conf:BiSqlWhereRelationship:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      :data="BiSqlWhereRelationshipList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <!-- <el-form-item prop="isReferenced" label="是否引用"></el-form-item> -->
      <el-table-column label="是否引用" align="center" prop="isReferenced" />
      <el-table-column label="查询语句ID" align="center" prop="sqlId" />
      <el-table-column label="条件ids" align="center" prop="conditionIds" />
      <el-table-column label="子条件ids" align="center" prop="childIds" />
      <el-table-column label="关系" align="center" prop="relationship" />
      <el-table-column
        label="合成条件预览"
        align="center"
        prop="conditionPreview"
      >
        <template slot-scope="scope">
          <mypopover :popoverInput="scope.row.conditionPreview" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['conf:BiSqlWhereRelationship:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['conf:BiSqlWhereRelationship:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改查询命令WHERE条件关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="查询语句ID" prop="sqlId">
          <el-input v-model="form.sqlId" placeholder="请输入查询语句ID" />
        </el-form-item> -->
        <el-form-item prop="isReferenced" label="是否引用">
          <el-radio-group
            v-model="form.isReferenced"
            size="small"
            fill="#66b1ff"
          >
            <el-radio :label="'是'">是</el-radio>
            <el-radio :label="'否'">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="条件" prop="conditionNameList">
          <!-- <el-input v-model="form.conditionIds" placeholder="请输入条件ids" /> -->
          <el-select
            v-model="form.conditionNameList"
            clearable
            filterable
            multiple
          >
            <el-option
              v-for="item in BiSqlWhereDwdList"
              :key="item.id"
              :label="`${item.conditionName}`"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合成条件" prop="childNameList">
          <!-- <el-input v-model="form.childNameList" placeholder="合成条件" /> -->
          <el-select v-model="form.childNameList" clearable filterable multiple>
            <el-option
              v-for="item in BiSqlWhereRelationshipList"
              :key="item.id"
              :label="`${item.conditionPreview}`"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关系" prop="relationship">
          <!-- <el-input v-model="form.relationship" placeholder="请输入关系" /> -->
          <el-select
            v-model="form.relationship"
            clearable
            filterable
            remote
            allow-create
          >
            <el-option
              v-for="item in orAndList"
              :key="item"
              :label="`${item}`"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiSqlWhereRelationship,
  getBiSqlWhereRelationship,
  delBiSqlWhereRelationship,
  addBiSqlWhereRelationship,
  updateBiSqlWhereRelationship,
} from "@/api/conf/sql/BiSqlWhereRelationship";
import {
  listBiSqlWhereDwd,
  getBiSqlWhereDwd,
  delBiSqlWhereDwd,
  addBiSqlWhereDwd,
  updateBiSqlWhereDwd,
} from "@/api/conf/sql/BiSqlWhereDwd";
export default {
  name: "BiSqlWhereRelationship",
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      // 查询命令筛选条件表格数据
      BiSqlWhereDwdList: [],
      orAndList: ["or", "and"],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询命令WHERE条件关系表格数据
      BiSqlWhereRelationshipList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sqlId: null,
        conditionIds: null,
        childIds: null,
        relationship: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  watch: {
    id: {
      handler: function (newnew, oldold) {
        console.log("newnew", newnew);
        if (newnew) {
          this.queryParams.sqlId = newnew;
          // this.form.sqlId = newnew;

          this.getList();
          this.getBiSqlWhereDwd();
          // this.gettableNameList();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    // this.getList();
  },
  methods: {
    getBiSqlWhereDwd() {
      listBiSqlWhereDwd(this.queryParams).then((response) => {
        this.BiSqlWhereDwdList = response.rows ? response.rows : [];
      });
    },
    /** 查询查询命令WHERE条件关系列表 */
    getList() {
      this.loading = true;
      listBiSqlWhereRelationship(this.queryParams).then((response) => {
        this.BiSqlWhereRelationshipList = response.rows ? response.rows : [];
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sqlId: null,
        // conditionIds: null,
        // childIds: null,
        relationship: null,
        isReferenced: "否",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加查询命令WHERE条件关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiSqlWhereRelationship(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改查询命令WHERE条件关系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.sqlId = this.id;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiSqlWhereRelationship(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiSqlWhereRelationship(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除查询命令WHERE条件关系编号为"' + ids + '"的数据项？'
        )
        .then(function () {
          return delBiSqlWhereRelationship(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "conf/BiSqlWhereRelationship/export",
        {
          ...this.queryParams,
        },
        `BiSqlWhereRelationship_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
